package com.greenantmall.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.bean.EMCompany;
import com.greenantmall.bean.EMRecv;
import com.greenantmall.bean.StoreInfo;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ExpressYiQuAdapter extends BaseAdapter {
	private Context mContext;
	private SimpleDateFormat mSimpleDateFormat;
	private List<EMRecv> mDataList;

	public ExpressYiQuAdapter(Context context, List<EMRecv> dataList) {
		mContext = context;
		mDataList = dataList;
		mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	}

	@Override
	public int getCount() {
		if (mDataList != null) {
			return mDataList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View child = convertView;
		ViewHolder holder = null;
		if (child == null) {
			child = LayoutInflater.from(mContext).inflate(
					R.layout.express_yiqu_list_item, null);
			holder = new ViewHolder(child);
			child.setTag(holder);
		} else {
			holder = (ViewHolder) child.getTag();
		}

		EMRecv emRecv = this.mDataList.get(position);

		holder.setPosition(position);
		// store detail
		holder.getLlStoreDetailLayout().setVisibility(View.GONE);
		holder.getIvArrow().setImageResource(R.drawable.drop_down);
		// em name & des
		EMCompany emCompany = emRecv.getEMCompany();
		if (emCompany != null && emCompany.getCompanyName() != null) {
			holder.getTvEmName().setText(emCompany.getCompanyName());
			holder.getTvEmDes().setText(
					emCompany.getCompanyName()
							+ mContext.getResources().getString(
									R.string.express_tab_baoguo));
		} else {
			holder.getTvEmName().setText("");
			holder.getTvEmDes().setText("");
		}
		// em code
		String emCode = emRecv.getEMCode();
		holder.getTvEmCode().setText(emCode==null ? "" : emCode);
		// em pic
		holder.getIvEmPic().setImageResource(R.drawable.ic_launcher);
		if (emCompany != null
				&& !TextUtils.isEmpty(emCompany.getCompanyImageUrl())) {
			ImageLoader.getInstance().displayImage(
					emCompany.getCompanyImageUrl(), holder.getIvEmPic(),
					AntHomeApplication.imageDisplayOptions1);
		}
		// tihuo code
		String tihuoStr = mContext.getResources().getString(
				R.string.express_tab_tihuo_code);
		String tihuoCode = emRecv.getEMCode();
		if (tihuoCode != null) {
			holder.getTvTihuoCode().setText(tihuoStr + tihuoCode);
		} else {
			holder.getTvTihuoCode().setText(tihuoStr + "");
		}
		// daozhan time
		Date daozhanDate = emRecv.getCreatedAt();
		if (daozhanDate != null) {
			Calendar daozhanCalendar = Calendar.getInstance();
			daozhanCalendar.setTime(daozhanDate);
			String format = mContext.getResources().getString(
					R.string.express_tab_daozhan_time);
			holder.getTvEmTime().setText(
					String.format(format, daozhanCalendar.get(Calendar.MONTH),
							daozhanCalendar.get(Calendar.DAY_OF_MONTH)));
		} else {
			holder.getTvEmTime().setText("");
		}
		// daozhan days
		Date qujianDate = emRecv.getSingedTime();
		String qujianStr = mContext.getResources().getString(
				R.string.express_tab_qujian_time);
		if (qujianDate != null) {
			holder.getTvQujianTime().setText(qujianStr + mSimpleDateFormat.format(qujianDate));
		} else {
			holder.getTvQujianTime().setText(qujianStr + "");
		}
		// store addr
		StoreInfo storeInfo = emRecv.getStore();
		if (storeInfo != null) {
			// store name
			String storeName = storeInfo.getDescrip();
			holder.getTvStoreName().setText(storeName == null ? "" : storeName);
			// store addr
			String storeAddr = storeInfo.getAddress();
			holder.getTvStoreAddr().setText(storeAddr == null ? "" : storeAddr);
			// store phone
			String storePhone = storeInfo.getPhone();
			holder.getTvStorePhone().setText(
					storePhone == null ? "" : storePhone);
			// store time
			String openTimeStr = storeInfo.getOpenTimeStr();
			String closeTimeStr = storeInfo.getCloseTimeStr();
			holder.getTvStoreTime().setText(openTimeStr + "~" + closeTimeStr);
		} else {
			// store name
			holder.getTvStoreName().setText("");
			// store addr
			holder.getTvStoreAddr().setText("");
			// store phone
			holder.getTvStorePhone().setText("");
			// store time
			holder.getTvStoreTime().setText("");
		}

		return child;
	}

	private class ViewHolder implements OnClickListener {
		private View mContentView;
		private TextView mTvEmName;
		private TextView mTvEmCode;
		private TextView mTvEmTime;
		private TextView mTvEmDes;
		private TextView mTvTihuoCode;
		private TextView mTvDaozhanDays;
		private TextView mTvQujianTime;
		private TextView mTvStoreName;
		private TextView mTvStoreAddr;
		private TextView mTvStorePhone;
		private TextView mTvStoreTime;

		private Button mBtnQujian;
		private ImageView mIvEmPic;
		private ImageView mIvArrow;
		private LinearLayout mLlStoreDetail;
		private LinearLayout mLlStoreDetailLayout;

		private int mPosition;

		public ViewHolder(View contentView) {
			mContentView = contentView;
			this.getLlStoreDetail().setOnClickListener(this);
			this.getBtnQujian().setOnClickListener(this);
		}

		public void setPosition(int position) {
			this.mPosition = position;
		}

		public int getPosition() {
			return mPosition;
		}

		public TextView getTvEmName() {
			if (mTvEmName == null) {
				mTvEmName = (TextView) mContentView
						.findViewById(R.id.express_em_name);
			}
			return mTvEmName;
		}

		public TextView getTvEmCode() {
			if (mTvEmCode == null) {
				mTvEmCode = (TextView) mContentView
						.findViewById(R.id.express_em_code);
			}
			return mTvEmCode;
		}

		public TextView getTvEmTime() {
			if (mTvEmTime == null) {
				mTvEmTime = (TextView) mContentView
						.findViewById(R.id.express_em_time);
			}
			return mTvEmTime;
		}

		public TextView getTvEmDes() {
			if (mTvEmDes == null) {
				mTvEmDes = (TextView) mContentView
						.findViewById(R.id.express_em_des);
			}
			return mTvEmDes;
		}

		public TextView getTvTihuoCode() {
			if (mTvTihuoCode == null) {
				mTvTihuoCode = (TextView) mContentView
						.findViewById(R.id.express_em_tihuo_code);
			}
			return mTvTihuoCode;
		}

		public TextView getTvDaozhanDays() {
			if (mTvDaozhanDays == null) {
				mTvDaozhanDays = (TextView) mContentView
						.findViewById(R.id.express_em_daozhan_days);
			}
			return mTvDaozhanDays;
		}

		public TextView getTvQujianTime() {
			if (mTvQujianTime == null) {
				mTvQujianTime = (TextView) mContentView
						.findViewById(R.id.express_em_qujian_time);
			}
			return mTvQujianTime;
		}

		public TextView getTvStoreName() {
			if (mTvStoreName == null) {
				mTvStoreName = (TextView) mContentView
						.findViewById(R.id.express_em_store_name);
			}
			return mTvStoreName;
		}

		public TextView getTvStoreAddr() {
			if (mTvStoreAddr == null) {
				mTvStoreAddr = (TextView) mContentView
						.findViewById(R.id.express_em_store_addr);
			}
			return mTvStoreAddr;
		}

		public TextView getTvStorePhone() {
			if (mTvStorePhone == null) {
				mTvStorePhone = (TextView) mContentView
						.findViewById(R.id.express_em_store_phone);
			}
			return mTvStorePhone;
		}

		public TextView getTvStoreTime() {
			if (mTvStoreTime == null) {
				mTvStoreTime = (TextView) mContentView
						.findViewById(R.id.express_em_store_time);
			}
			return mTvStoreTime;
		}

		public Button getBtnQujian() {
			if (mBtnQujian == null) {
				mBtnQujian = (Button) mContentView
						.findViewById(R.id.express_em_btn_qujian);
			}
			return mBtnQujian;
		}

		public ImageView getIvEmPic() {
			if (mIvEmPic == null) {
				mIvEmPic = (ImageView) mContentView
						.findViewById(R.id.express_em_pic);
			}
			return mIvEmPic;
		}

		public LinearLayout getLlStoreDetail() {
			if (mLlStoreDetail == null) {
				mLlStoreDetail = (LinearLayout) mContentView
						.findViewById(R.id.express_em_ll_1);
			}
			return mLlStoreDetail;
		}
		
		public ImageView getIvArrow() {
			if (mIvArrow == null) {
				mIvArrow = (ImageView) mContentView
						.findViewById(R.id.express_em_store_arrow);
			}
			return mIvArrow;
		}

		public LinearLayout getLlStoreDetailLayout() {
			if (mLlStoreDetailLayout == null) {
				mLlStoreDetailLayout = (LinearLayout) mContentView
						.findViewById(R.id.express_em_ll_2);
			}
			return mLlStoreDetailLayout;
		}

		@Override
		public void onClick(View v) {
			if (v.equals(this.getLlStoreDetail())) {
				if (this.getLlStoreDetailLayout().getVisibility() == View.VISIBLE) {
					this.getLlStoreDetailLayout().setVisibility(View.GONE);
					this.getIvArrow().setImageResource(R.drawable.drop_down);
				} else {
					this.getLlStoreDetailLayout().setVisibility(View.VISIBLE);
					this.getIvArrow().setImageResource(R.drawable.drop_up);
				}
			} else if (v.equals(this.getBtnQujian())) {

			}
		}
	}

}
